﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
using System.Text.RegularExpressions;

namespace OpenLightGroup.Common.Converters
{
    public class BooleanDataFormCommandButtonConverter
        : IValueConverter
    {

        protected static DataFormCommandButtonsVisibility? GetButtonValues(string parameter)
        {
            DataFormCommandButtonsVisibility? output = null;

            if (parameter != null)
            {

                List<string> defaultValues = new List<string>();

                parameter = parameter.Replace("(", string.Empty).Replace(")", string.Empty);

                if (parameter.ToString().Contains("|"))
                {

                    defaultValues = parameter.ToString().Split("|".ToCharArray()).ToList();

                }
                else
                {

                    defaultValues.Add(parameter.ToString());
                }

                foreach (var paramValue in defaultValues)
                {

                    if (Enum.IsDefined(typeof(DataFormCommandButtonsVisibility), paramValue))
                    {

                        DataFormCommandButtonsVisibility thisValue = (DataFormCommandButtonsVisibility)Enum.Parse
                                                 (typeof(DataFormCommandButtonsVisibility), paramValue, true);

                        if (output == null)
                        {
                            output = thisValue;
                        }
                        else
                        {
                            output |= thisValue;
                        }


                    }

                }
            }
            return output;
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var buttonGroups = Regex.Matches(parameter.ToString(), @"\(\w+\)");

            if (buttonGroups.Count == 2)
            {

                var trueButtonGroup = GetButtonValues(buttonGroups[0].Value);
                var falseButtonGroup = GetButtonValues(buttonGroups[1].Value);
         
                bool boolValue = true;

                if (value != null && !string.IsNullOrEmpty(value.ToString()))
                {

                    if (!bool.TryParse(value.ToString(), out boolValue))
                    {
                        boolValue = true;
                    }


                }

                return boolValue ? trueButtonGroup : falseButtonGroup;

            }

            else
            {

                return null;

            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
